package com.wms.crypt;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class BrowserEncryption {
	
	private static final String PUBLICKEY  = "classpath:Browser_Rsa_PbKey.dat";
	private static final String PRIVATEKEY = "classpath:Browser_Rsa_PrKey.dat";
	
	private RSAPublicKey   pbkey;   //public key  
	private RSAPrivateKey  prkey;  //private key
	
	private Logger logger = Logger.getLogger(BrowserEncryption.class);
	
//	pbkey.getModulus()          //n(1)
//	pbkey.getPublicExponent()   //e
//	prkey.getModulus()          //n(2) n1==n2
//	prkey.getPrivateExponent()  //d
	
	@PostConstruct
	public void init()throws Exception{
//		generateKey();
//		coutKeys();
	}
	
	
	//生成密钥
	public void generateKey()throws Exception{
		
		//创建密钥对生成器，指定加密和解密算法为RSA
        KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
        
        //指定密钥的长度，初始化密钥对生成器
        kpg.initialize(1024);
        
        //生成密钥对
        KeyPair kp=kpg.genKeyPair();    
        
        //获取公钥
        pbkey=(RSAPublicKey )kp.getPublic();
        
        //获取私钥
        prkey=(RSAPrivateKey)kp.getPrivate();
        
        
       //保存公钥到文件
        ObjectOutputStream objectPbkey=new ObjectOutputStream(new FileOutputStream(PUBLICKEY));
        objectPbkey.writeObject(pbkey);
     
        //保存私钥到文件
        ObjectOutputStream objectPrkey=new ObjectOutputStream(new FileOutputStream(PRIVATEKEY));
        objectPrkey.writeObject(prkey);
        
        objectPbkey.close();
        objectPrkey.close();
        
	}
	
	
	//获取公钥n(1)，以hex16进制返回
	public String getKeyN(){
		
		byte[] bytes = pbkey.getModulus().toByteArray();
		String hv;
		
		 StringBuilder stringBuilder = new StringBuilder("");
		 
		 for (int i = 0; i < bytes.length; i++) {  
	           int v = bytes[i] & 0xFF;  
	           hv = Integer.toHexString(v);  
	           if (hv.length() < 2) {  
	               stringBuilder.append(0);  
	           }  
	           stringBuilder.append(hv);
	       }  
	       return stringBuilder.toString(); 
	}
		
		
	//获取公钥e，以hex16进制返回
	public String getKeyE(){
		
		byte[] bytes = pbkey.getPublicExponent().toByteArray();
		String hv;
		
		 StringBuilder stringBuilder = new StringBuilder("");
		 
		 for (int i = 0; i < bytes.length; i++) {  
	           int v = bytes[i] & 0xFF;  
	           hv = Integer.toHexString(v);  
	           if (hv.length() < 2) {  
	               stringBuilder.append(0);  
	           }  
	           stringBuilder.append(hv);
	       }  
	       return stringBuilder.toString(); 
	}	
	
	
	private void coutKeys(){
//		pbkey.getModulus()          //n(1)
//		pbkey.getPublicExponent()   //e
//		prkey.getModulus()          //n(2) n1==n2
//		prkey.getPrivateExponent()  //d
		
		logger.debug("************* browser keys *******************");
		
		logger.debug("pbk n -- "+pbkey.getModulus());
		logger.debug("pbk e -- "+pbkey.getPublicExponent());
		logger.debug("prk n -- "+prkey.getModulus());
		logger.debug("prk d -- "+prkey.getPrivateExponent());
	}
	
}
